from wsgiref.simple_server import make_server

def application( environ, start_response):
    # response_body in a list
    print environ
    response_body = ['%s:  %s' %(key, value)
        for key, value in sorted(environ.items())]
    response_body = '\n'.join(response_body)
    response_body = ['The Begining\n',
                        '*' * 30 + '\n',
                        response_body,
                        '\n' + '*' * 30 ,
                        '\nThe End']

    content_length = 0
    for s in response_body:
        content_length += len(s)

    # status code and response_headers
    status = '200 OK'
    response_headers =[('Content-Type', 'text/plain'),
        ('Content-Length', str(content_length))]
    # use callback function to send back status code
    # and response headers
    start_response(status, response_headers)
    # return response body thruogh return statement
    return response_body


httpd = make_server('localhost',8051,application)
# httpd.handle_request()
httpd.serve_forever()  # listen http request
